package cn.com.jslh.boarpro.main.view;

import android.app.Activity;
import android.bluetooth.le.AdvertiseData;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.json.JSONException;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.main.entity.NewsWarnEnity;
import cn.com.jslh.boarpro.main.entity.WarnResultBody;
import cn.com.jslh.boarpro.main.entity.WarnResultEntity;
import cn.com.jslh.boarpro.main.model.WarnResultModelImpl;
import cn.com.jslh.boarpro.main.presenter.WarnResultPresenterImpl;
import cn.com.jslh.boarpro.main.view.view.WarnResultView;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.adapter.WarnResultAdapter;

//预警结果
public class WarnResultActivity extends AppCompatActivity {
    @BindView(R.id.recycle_warn_result)
    RecyclerView recyclerView;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.title_text)
     TextView title;
    @BindView(R.id.back)
     ImageView back;
    @BindView(R.id.layout_warn_result_title)
    LinearLayout layout;
     WarnResultPresenterImpl presenter;
    @BindView(R.id.swipRefreshLayout)
    SwipeRefreshLayout sw;
    private WarnResultBody head;
    private NewsWarnEnity info1;
    private WarnResultEntity info;
    private WarnResultAdapter adapter;
    private boolean isErr; //是否成功获取数据
    private int pageIndex=0;
    private List<String> widthList;
    private String lineId,itemId;
    private int page=1;
    private TextView bh,eh,pz,dn,tc1,tc2,tc3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_warn_result);
        ButterKnife.bind(this);

        initUI();

        initRefreshLayout();


        load();





    }


    private void initUI(){
        Intent intent=getIntent();
        info1= (NewsWarnEnity) intent.getSerializableExtra("info");
        title.setText(info1.getContent());
        lineId=SPUtils.getInstance().getString(BaseApplication.FLINEID);
//        lineId="kvYAAABMvoYB5Kp+";
        itemId=info1.getId();
        String[] param={SPUtils.getInstance().getString(BaseApplication.FLINEID),info1.getId()};
//        String[] param={"kvYAAABMvoYB5Kp+",info.getId()};
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);

        WindowManager manager = getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        adapter=new WarnResultAdapter(R.layout.warn_result_item,width);

        recyclerView.setLayoutManager(linearLayoutManager);

        adapter.disableLoadMoreIfNotFullPage(recyclerView);
        recyclerView.setAdapter(adapter);
        getData(1);

    }


    private void initHead(WarnResultBody head){
        View view = getLayoutInflater().inflate(R.layout.warn_result_title_item, (ViewGroup) recyclerView.getParent(), false);
        bh= (TextView) findViewById(R.id.tv_bh);
        eh= (TextView) findViewById(R.id.tv_eh);
        pz= (TextView) findViewById(R.id.tv_pz);
        dn= (TextView) findViewById(R.id.tv_dn);
        tc1=(TextView)findViewById(R.id.tv_tc1);
        tc2=(TextView)findViewById(R.id.tv_tc2);
        tc3=(TextView)findViewById(R.id.tv_tc3);
        List<String> title=head.getHeadTitle();
        bh.setText(title.get(0));
        eh.setText(title.get(1));
        pz.setText(title.get(2));
        dn.setText(title.get(3));
        tc1.setText(title.get(4));
        tc2.setText(title.get(5));
        tc3.setText(title.get(6));
    }

    private void initRefreshLayout(){
        sw.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    private void refresh(){
        page=1;
        adapter.setEnableLoadMore(false);
        new WarnResultModelImpl(WarnResultActivity.this).getWarnResultData(new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                Log.d("page2", page + "zxx");
                info = JSON.parseObject(s, WarnResultEntity.class);
                pageIndex = info.getData().getIsLastPage();
                if (pageIndex == 0) {
                    Log.d("pageIndex2", pageIndex + "zxx");
                    head = info.getData().getHead();
                    List<WarnResultBody> bodyList = info.getData().getBody();
                    widthList = info.getData().getHead().getColWidth();
                    adapter.addData(bodyList, true);
                    adapter.notifyDataSetChanged();
                    sw.setRefreshing(false);
                    adapter.setEnableLoadMore(true);
                }


            }
        }, new WebService.NetFaildListener() {
            @Override
            public void Faild(String str) {
                isErr=false;
                sw.setRefreshing(false);
                adapter.setEnableLoadMore(true);
            }
        },lineId, itemId, page);

    }


    private void load(){
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                Log.d("加载更多","加载");
//                adapter.setEnableLoadMore(true);
                sw.setRefreshing(false);
                if(pageIndex!=0){
                    adapter.loadMoreEnd();
                }else {
                    if(isErr){
                     loadMore();
                    }else {
                        adapter.loadMoreFail();
                    }
                }
            }

        },recyclerView);


    }

    @OnClick(R.id.back)
    public void onViewClicked(){
        WarnResultActivity.this.finish();
    }



    public void getData(final int page){
        new WarnResultModelImpl(WarnResultActivity.this).getWarnResultData(new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                Log.d("s", s);
                isErr=true;
                info = JSON.parseObject(s, WarnResultEntity.class);
                pageIndex = info.getData().getIsLastPage();
                Log.d("pageIndex", pageIndex + "zxx");
                head = info.getData().getHead();
                List<WarnResultBody> bodyList = info.getData().getBody();
                widthList = info.getData().getHead().getColWidth();
                adapter.setDwidth(widthList);
                adapter.addData(bodyList, true);
                adapter.notifyDataSetChanged();
                initHead(head);
                adapter.setEnableLoadMore(true);
                sw.setRefreshing(false);

            }
        }, new WebService.NetFaildListener() {
            @Override
            public void Faild(String str) {
                isErr=false;
            }
        },lineId, itemId, page);
    }





    public void upData(final int page,final boolean refresh){
        new WarnResultModelImpl(WarnResultActivity.this).getWarnResultData(new WebService.NetSuccessListener() {
            @Override
            public void Success(String s) throws JSONException {
                Log.d("page2", page + "zxx");
                if(s!=null){
                    isErr=true;
                    info = JSON.parseObject(s, WarnResultEntity.class);
                    pageIndex = info.getData().getIsLastPage();
                    Log.d("pageIndex2", pageIndex + "zxx");
                    head = info.getData().getHead();
                    List<WarnResultBody> bodyList = info.getData().getBody();
                    widthList = info.getData().getHead().getColWidth();
                    adapter.addData(bodyList, refresh);
                    adapter.notifyDataSetChanged();

//                    adapter.loadMoreComplete();
//                    sw.setRefreshing(false);
                }else {
                    isErr=false;
                }

                }


        }, new WebService.NetFaildListener() {
            @Override
            public void Faild(String str) {
                isErr=false;
                adapter.loadMoreFail();
            }
        },lineId, itemId, page);
    }


    private void loadMore(){
        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                page+=1;
                Log.d("打印page",page+"zxx");
                upData(page,false);
                adapter.loadMoreComplete();
            }
        },2000);
    }


}
